package com.whiteape.book.ui.fragment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.SwitchPreference;

import com.hwangjr.rxbus.RxBus;
import com.whiteape.book.R;
import com.whiteape.book.common.RxEvent;
import com.whiteape.book.core.manager.ReadSettingManager;
import com.whiteape.book.model.dto.CollectionMediaBean;
import com.whiteape.book.utils.SharedPreUtils;
import com.whiteape.book.widget.PreferenceFragmentPlus;

public class SettingsFragment extends PreferenceFragmentPlus {

    //和ReadSettingManager公用文件
    private static final String PREF_FILE_NAME = "IReader_pref";
    private SwitchPreference useWebDavPref;
    private EditTextPreference webDavUrlPref, webDavAccountPref, webDavPasswordPref;

    private Preference.OnPreferenceChangeListener onPreferenceChangeListener = (Preference preference, Object value) -> {
        String stringValue = value.toString();

        if (preference instanceof ListPreference) {
            ListPreference listPreference = (ListPreference) preference;
            int index = listPreference.findIndexOfValue(stringValue);
            // Set the summary to reflect the new value.
            preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
            if(preference.getKey().equals("shared_coll_order_type")){
                if(Integer.parseInt(stringValue) != ReadSettingManager.getInstance().getCollOrderType()){
                    //书架排序方式变了
                    RxBus.get().post(RxEvent.MEDIA_COLLECTION_REFRESH, new CollectionMediaBean());
                }
            }
        } else if (preference instanceof SwitchPreference) {
            if (preference.getKey().equals("shared_use_web_dav")) {
                boolean enabled = (boolean) value;
                if (webDavUrlPref != null) {
                    webDavUrlPref.setEnabled(enabled);
                }
                if (webDavAccountPref != null) {
                    webDavAccountPref.setEnabled(enabled);
                }
                if (webDavPasswordPref != null) {
                    webDavPasswordPref.setEnabled(enabled);
                }
            }

        } else {
            // For all other preferences, set the summary to the value's
            if (value.toString().trim().length() > 0) {
                if (preference.getKey().contains("password")) {
                    preference.setSummary("已设置");
                } else {
                    preference.setSummary(stringValue);
                }
            }else{
                if(preference.getKey().contains("password")){
                    preference.setSummary("WebDav服务器应用密码");
                }
            }

        }
        return true;
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RxBus.get().register(this);
    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        //下面两句不能更换顺序，否则没有数据
        getPreferenceManager().setSharedPreferencesName(PREF_FILE_NAME);
        addPreferencesFromResource(R.xml.prefs_setting);

        useWebDavPref = findPreference("shared_use_web_dav");
        webDavUrlPref = findPreference("shared_web_dav_url");
        webDavAccountPref = findPreference("shared_web_dav_account");
        webDavPasswordPref = findPreference("shared_web_dav_password");
        bindPreferenceSummaryToValue(useWebDavPref, true);
        bindPreferenceSummaryToValue(webDavUrlPref);
        bindPreferenceSummaryToValue(webDavAccountPref);
        bindPreferenceSummaryToValue(webDavPasswordPref);
        bindPreferenceSummaryToValue(findPreference("shared_coll_order_type"));


    }

    private void bindPreferenceSummaryToValue(Preference preference) {
        // Set the listener to watch for value changes.
        bindPreferenceSummaryToValue(preference, false);
    }

    private void bindPreferenceSummaryToValue(Preference preference, boolean isBoolean) {
        // Set the listener to watch for value changes.
        preference.setOnPreferenceChangeListener(onPreferenceChangeListener);
        onPreferenceChangeListener.onPreferenceChange(preference,
                isBoolean ? SharedPreUtils.getInstance().getBoolean(preference.getKey(), false) : SharedPreUtils.getInstance().getString(preference.getKey()));
    }

//    @Override
//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        View parent = super.onCreateView(inflater, container, savedInstanceState);
//        if (null != parent) {
//            View listView = parent.findViewById(android.R.id.list);
//            if (null != listView) {
//                listView.setPadding(0, listView.getPaddingTop(), 0, listView.getPaddingBottom());
//            }
//        }
//        return parent;
////        return super.onCreateView(inflater, container, savedInstanceState);
//    }


    @Override
    public void onDetach() {
        super.onDetach();
        RxBus.get().unregister(this);
    }
}
